//
//  ChangePhoneSecondViewController.m
//  huashiedu
//
//  Created by oceans on 2018/9/8.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ChangePhoneSecondViewController.h"
#import "Utility.h"
#import "TipManager.h"

@interface ChangePhoneSecondViewController ()

@property (strong, nonatomic) UITextField *username;
@property (strong, nonatomic) UITextField *password;
@property (strong, nonatomic) UIButton *getCodeButton;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) double timertime;
@end

@implementation ChangePhoneSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"绑定密保手机";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    self.username = [[UITextField alloc] initWithFrame:CGRectMake(31, XNavigationHeight + 30, ScreenWidth - 62, 35)];
    _username.placeholder = @"请输入手机号";
    _username.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:_username];
    
    CALayer *topline = [[CALayer alloc] init];
    topline.frame = CGRectMake(30, CGRectGetMaxY(_username.frame) + 6.5, ScreenWidth - 60, 0.5);
    topline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:topline];
    
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(topline.frame) + 17, ScreenWidth - 62, 35)];
    _password.placeholder = @"请输入验证码";
    [self.view addSubview:_password];
    
    CALayer *botline = [[CALayer alloc] init];
    botline.frame = CGRectMake(30, CGRectGetMaxY(_password.frame) + 6.5, ScreenWidth - 60, 0.5);
    botline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:botline];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(31, CGRectGetMaxY(botline.frame) + 35.0, ScreenWidth - 62, 50);
    login.backgroundColor = COLOR(253, 119, 77, 1);
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [login setTitle:@"确定" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    login.layer.cornerRadius = 3.0;
    login.layer.masksToBounds = YES;
    [self.view addSubview:login];
    
    self.getCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _getCodeButton.frame = CGRectMake(0, 0, 113, 33);
    [_getCodeButton setTitleColor:COLOR(151, 151, 151, 1) forState:UIControlStateNormal];
    _getCodeButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_getCodeButton addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
    _password.rightView = _getCodeButton;
    _password.rightViewMode = UITextFieldViewModeAlways;
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}

- (void)done {
    
    if (_isThirdLogin) {
        
        if ([Utility checkMobile:_username.text] && _password.text.length > 0) {
            [self.view endEditing:YES];
            __weak typeof(self) weakself = self;
            [[UserModel shared] thirdPartyBindPhone:_username.text code:_password.text connect_id:_connect_id successBlock:^{
                [TipManager showTip:@"绑定成功"];
                [weakself.navigationController popToRootViewControllerAnimated:YES];
            }];
        }
        
    }else {
        if ([Utility checkMobile:_username.text] && _password.text.length > 0) {
            
            [self.view endEditing:YES];
            __weak typeof(self) weakself = self;
            [[UserModel shared] bindingNewPhone:_username.text code:_password.text successBlock:^{
                [TipManager showTip:@"修改成功"];
                [weakself.navigationController popToRootViewControllerAnimated:YES];
            }];
        }
    }
}

- (void)getCode {
    
    if ([Utility checkMobile:_username.text]) {
        [self.view endEditing:YES];
        _timertime = 60;
        [self startTimer];
        __weak typeof(self) weakself = self;
        [[UserModel shared] getCodeWithPhoneNumber:_username.text scope:@"bind" successBlock:nil failedBlock:^{
            
            [weakself stopTimer];
        }];
        
    }
}

- (void)startTimer {
    
    _getCodeButton.enabled = NO;
    [_getCodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}

- (void)timerMethod {
    
    if (_timertime <= 0) {
        [_getCodeButton setTitleColor:COLOR(235, 86, 0, 1) forState:UIControlStateNormal];
        [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeButton.enabled = YES;
        [self.timer invalidate];
        return;
    }
    _timertime = _timertime - 1;
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
}

- (void)stopTimer {
    [_getCodeButton setTitleColor:COLOR(235, 86, 0, 1) forState:UIControlStateNormal];
    [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _getCodeButton.enabled = YES;
    [self.timer invalidate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
